namespace Yaaf.WP7.NativeAccess.Helper
{
    using System;
    using System.Linq;

    using Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_RegRw;
    using Yaaf.WP7.NativeAccess.Registry;
    using Yaaf.WP7.NativeAccess.Registry.Internals.AllDevices;

    public class DeviceSwitcher
    {
        private static DeviceManufacturer? deviceType;

        public static DeviceManufacturer DeviceType
        {
            get
            {
                if (deviceType == null)
                {
                    deviceType = GetDeviceType();
                }

                return deviceType.Value;
            }
        }

        private static DeviceManufacturer GetDeviceType()
        {
            RegistryBase ce;
            try
            {
                ce = new RegistryBase();
            }
            catch (RegisterComException e)
            {
                return DeviceManufacturer.Unknown;
            }
            var subkeys = ce.GetSubKeyNames(RootKey.E_HKEY_LOCAL_MACHINE, "Software");
            if (subkeys.Contains("Samsung"))
            {
                return DeviceManufacturer.Samsung;
            }
            else if (subkeys.Contains("LGE"))
            {
                return DeviceManufacturer.LG;
            }
            else if (subkeys.Contains("HTC"))
            {
                return DeviceManufacturer.HTC;
            }
            else if (subkeys.Contains("Dell"))
            {
                return DeviceManufacturer.Dell;
            }
            else if (subkeys.Contains("ASUS"))
            {
                return DeviceManufacturer.ASUS;
            }

            /* Old method
            try
            {
                using (var reg = new HTCRegistry())
                {
                    if (reg.IsHTCDevice()) return DeviceManufacturer.HTC;
                }
            }
            catch (RegisterComException)
            {
            }
            */
            return DeviceManufacturer.Unknown;
        }
    }

    public enum DeviceManufacturer
    {
        Unknown,
        HTC,
        Samsung,
        LG,
        Dell,
        ASUS,
    }
}